<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/3
 * Time: 17:15
 */
namespace App\Models;

use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;
use Illuminate\Database\Eloquent\Model;

class VersionCdnManager extends Model{

    static $get_manager_list_url = "/gm/get-manager-cdn-list";
    static $upgrade_manager_cdn_url = "/gm/update-cdn-used";

	public function paginate()
    {
    	$all_list = $this->get_manager_list_data();


        $page_size = $total = count($all_list)?:1;

        $data_list = array_slice($all_list, ($page-1)*$page_size, $page_size);
        // extract($data);

        // $data_tmp = static::hydrate((array)$subjects);

        // $list = static::hydrate($data_tmp);

        $paginator = new LengthAwarePaginator($data_list, $total, $page_size);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public function get_manager_list_data() {
        $m_type = Input::get('m_type');
        $url = env('APP_API_HOST'). self::$get_manager_list_url;
        $url = \App\Service\Common::build_url_query($url, ['m_type' => $m_type]);
        $source_rtn = file_get_contents($url);
        $data_list = json_decode($source_rtn, true);
        $data_list = $data_list['data'] ?:[];
        // \Log::debug("[VERSION_MANAGER][LIST]", [$data_list]);
        $source_data = [];
        foreach ($data_list as $attr => $value) {
            
            $ver_arr = explode(':', $attr);
            $ver_ac = explode(',', $value);
            
            $source_data[] = [
                'm_type' => $ver_arr[0],
                'version_code' => $ver_arr[1],
                'version_name' => $ver_ac[0],
                'value' => $ver_ac[1],
                'upgrade' => intval($ver_ac[2]),
                'guest' => intval($ver_ac[3]),
                'areas' => $ver_ac[4] ?explode('|',$ver_ac[4]) :[],
            ];
        }
        $last_sorts = array_column($source_data,'version_code');
        array_multisort($last_sorts, SORT_ASC, $source_data);
        \Log::debug("[VERSION_MANAGER][LIST]", [$source_data]);
        return $source_data;
    }

    public static function with($relations)
    {
        //APP_API_HOST
        return new static;
    }

    // 更新使用CDN
    public static function upgradeManagerCdn($m_type, $version_code, $version_name, $value = 0, $upgrade = 0) {
        \Log::info("[MANAGER_SAVE][PARAM]M_TYPE[$m_type]VERSION_CODE[$version_code]VALUE[$value]");
        $url = env('APP_API_HOST'). self::$upgrade_manager_cdn_url;
        $url = \App\Service\Common::build_url_query($url, ['m_type' => $m_type, 'version_code'=> $version_code, 'version_name' => $version_name, 'value'=>$value, 'upgrade'=> $upgrade]);
        $result = file_get_contents($url);
        \Log::info("[MANAGER_SAVE][PARAM]url[$url]");
        return json_decode($result ,true);
    }

    public static function getValueSelect(){
        return [
            '0' => '关闭CDN',
            '1' => '打开CDN',
            '-1' => '删除',
        ];
    }

    public static function getValueUpgradeSelect(){
        return [
            '0' => '版本不变',
            '1' => '强制更新',
        ];
    }

    public static function getValueOpenSelect(){
        return [
            '0' => '关闭',
            '1' => '开启',
        ];
    }
}